package at.fhooe.stefanirndorfer.controller;

import org.lwjgl.input.Keyboard;

import at.fhooe.stefanirndorfer.game.Game;
import at.fhooe.stefanirndorfer.input.KeyInputListener;
import at.fhooe.stefanirndorfer.labeling.Labeling;
import at.fhooe.stefanirndorfer.quadtree.QuadTree;
import at.fhooe.stefanirndorfer.quadtree.QuadTreeNode;
import at.fhooe.stefanirndorfer.walls.GeometrySetups;

import static at.fhooe.stefanirndorfer.utilities.Utilities.NE;
import static at.fhooe.stefanirndorfer.utilities.Utilities.NW;
import static at.fhooe.stefanirndorfer.utilities.Utilities.SE;
import static at.fhooe.stefanirndorfer.utilities.Utilities.SW;

public class Controller implements KeyInputListener{
	
	
	private QuadTree qtree;
	private Labeling l;
	private boolean geom_generated;
	private boolean regionsGenerated;
	
	public Controller(){
		geom_generated = false;
		regionsGenerated = false;
		qtree = null;
		l = null;
		setUpKeyInputListener();
		
		printKeyCommands();
	}
	

	private void printKeyCommands() {
		System.out.println("//__________________//");
		System.out.println("//__________________//");
		System.out.println("Key commands:");
		System.out.println("1: generate geometry");
		System.out.println("2: generate quad tree");
		System.out.println("3: label quadtree (not yet cleaned up)");
		System.out.println("4: label quadtree (clean up merges array)");
		System.out.println("");
		System.out.println("c: toggle leaf colors (label-color, single leaf color)");
		
		System.out.println("//__________________//");
		System.out.println("//__________________//");
		
	}


	@Override
	public void setUpKeyInputListener() {
		Game.getInstance().getInputManager().addKeyListener(this);
	}

	@Override
	public void tearDownKeyInputListener(KeyInputListener il) {
		Game.getInstance().getInputManager().removeKeyListener(this);
	}

	@Override
	public void processKeyInput(int eventKey, boolean keyDown) {
		if(keyDown){
			if(eventKey == Keyboard.KEY_1 && keyDown && !geom_generated){
				setUpGeometry();
			}
			if(eventKey == Keyboard.KEY_2 && keyDown && geom_generated){
				generateQuadTree();
			}
			if(eventKey == Keyboard.KEY_3 && keyDown && qtree != null){
				generateRegions();
			}
			if(eventKey == Keyboard.KEY_4 && keyDown && qtree != null && regionsGenerated){
				cleanUpMergesArray();
			}
			if(eventKey == Keyboard.KEY_C && qtree != null && geom_generated){
				toggleLeafColors(qtree.getRootNode());
			}
			
		}
	}

	private void cleanUpMergesArray() {
		l.merge();
	}


	private void toggleLeafColors(QuadTreeNode n) {
		if(n.getChildNodes()[0] != null){
			toggleLeafColors(n.getChildNodes()[NW]);
			toggleLeafColors(n.getChildNodes()[NE]);
			toggleLeafColors(n.getChildNodes()[SE]);
			toggleLeafColors(n.getChildNodes()[SW]);
		}else{
			n.toggleColors();
		}
	}


	private void generateRegions() {
		l = new Labeling(qtree.getRootNode());
		regionsGenerated = true;
	}


	private void generateQuadTree() {
		qtree = new QuadTree(20, 700, 10, new double[]{0.0, 0.0});
	}

	private void setUpGeometry() {
		GeometrySetups.GeometryOne();
		GeometrySetups.SimpleExampleGeometry();
		//GeometrySetups.GeometryTwo();
		geom_generated = true;
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return null;
	}

}
